﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SZHL.UI.AppCode;
using System.Reflection;
using FastReflectionLib;
using SZHL.Data;
using Newtonsoft.Json;
using System.Data;

namespace SZHL.UI.API
{
    public class CCQJManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(CCQJManage).GetMethod(msg.Action.ToUpper());
            CCQJManage model = new CCQJManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 添加出差请假
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">客户信息</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDCCQJ(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_CCXJ ccxj = JsonConvert.DeserializeObject<ZH_CCXJ>(P1);
            
            if (ccxj.Daycount == 0)
            {
                msg.ErrorMsg = "申请天数不能为空";
                return;
            }
            if(ccxj.EndTime<ccxj.StarTime)
            {
                msg.ErrorMsg = "开始时间不能大于结束时间";
                return;
            }

            if (ccxj.ID == 0)
            {
                ccxj.ShenQingRen = string.IsNullOrEmpty(ccxj.ShenQingRen) ? UserInfo.User.UserRealName : ccxj.ShenQingRen;
                ccxj.CRDate = DateTime.Now;
                ccxj.CRUser = UserInfo.User.UserName;
                ccxj.SPStatus = "0";
                ccxj.SPStatus1 = "0";
                ccxj.BranchName = new JH_Auth_BranchB().GetBranchNameByCode(UserInfo.User.BranchCode);
                ccxj.BranchNo = UserInfo.User.BranchCode;
                ccxj.intProcessStanceid = new Yan_WF_PDB().GetProcessID("请假出差");
                new ZH_CCXJB().Insert(ccxj);
            }
            else
            {
                new ZH_CCXJB().Update(ccxj);
            }
            msg.Result = ccxj;
        }
        /// <summary>
        /// 审批出差请假
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">出差请假Id</param>
        /// <param name="P2">意见</param>
        /// <param name="UserInfo"></param>
        public void SPCCQJ(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_CCXJ ccxj = new ZH_CCXJB().GetEntity(d => d.ID.ToString() == P1);
            string status = context.Request["spstatus"]; //状态

            if (ccxj.SPStatus != "0")
            {
                ccxj.SPStatus1 = status;
                ccxj.SPYJ1 = P2;
                ccxj.SPTime1 = DateTime.Now;
                ccxj.SPUser1 = UserInfo.User.UserName;
            }
            else
            {
                ccxj.SPStatus = status;
                ccxj.SPYJ = P2;
                ccxj.SPTime = DateTime.Now;
                ccxj.SPUser = UserInfo.User.UserName;
            }
            new ZH_CCXJB().Update(ccxj);
            msg.Result = ccxj;
        }
        /// <summary>
        /// 获取出差请假列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">类别</param>
        /// <param name="P2">请假内容</param>
        /// <param name="UserInfo"></param>
        public void GETCCQJLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " Where 1=1 ";
            if (P1 != "")
            {
                strWhere += string.Format(" And LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And  ZhuYaoShiYou like '%{0}%'", P2);
            }
            string b = context.Request.QueryString["b"] ?? "";
            if (b != "")
            {

                strWhere += string.Format(" And  branchNo= '{0}'", b);
            }
            string strSql = string.Format("select ShenQingRen,LeiBie,StarTime,EndTime,Daycount,ZhuYaoShiYou,ID,BranchName,SPStatus,SPStatus1 from ZH_CCXJ " + strWhere);
            msg.Result = new ZH_CCXJB().GetDTByCommand(strSql);
            msg.Result1 = new Yan_WF_TDB().GetTaskStatus(1);

        }
        /// <summary>
        /// 删除请假出差
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELCCQJBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {

                if (new ZH_CCXJB().Delete(d => d.ID.ToString() == P1))
                {
                    msg.ErrorMsg = "";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取出差请假信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETCCQJMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {

            msg.Result = new ZH_CCXJB().GetEntity(d => d.ID.ToString() == P1);
            msg.Result1 = new Yan_WF_TDB().GetTaskStatus(1);
        }
        /// <summary>
        /// 修改流程信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void MODITYDF(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            DFContent df = JsonConvert.DeserializeObject<DFContent>(P1);
            if (df.spRole == "")
            {
                msg.ErrorMsg = "第一级审核角色不能为空";
                return;
            }
            string strSql = string.Format(@"UPDATE Yan_WF_TD set AssignedRole={0},IsDetailUser='{4}' where ProcessDefinitionID={1} and Taskorder=2 
                                            UPDATE Yan_WF_TD set AssignedRole={2},isforbid='{3}' where ProcessDefinitionID={1} and Taskorder=3", df.spRole, df.PDId, df.spRole2, df.isEnable == false ? "Y" : "N", df.IsDetailUser);
            new Yan_WF_PDB().ExsSql(strSql);
        }
        /// <summary>
        /// 获取流程信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETDFLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = string.Format(" select AssignedRole,isforbid,IsDetailUser from dbo.Yan_WF_TD where ProcessDefinitionID={0} and Taskorder>1 ", P1);
            msg.Result = new Yan_WF_TDB().GetDTByCommand(strSql);
        }
        /// <summary>
        /// 获取第一次审批人员列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">流程名称</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETSPUSERLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = string.Format(@"select   u.UserRealName,u.UserName from  JH_Auth_UserRole ur INNER JOIN JH_Auth_User u on ur.UserName=u.UserName inner join Yan_WF_TD td on ur.RoleCode=td.AssignedRole
                                            inner join Yan_WF_PD pd on td.ProcessDefinitionID=pd.ProcessDefinitionID 
                                            where pd.ProcessName='{0}' and td.Taskorder=2", P1);
            msg.Result = new Yan_WF_TDB().GetDTByCommand(strSql);
        }
        /// <summary>
        /// 审批列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETSPCCQJLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " ";
            if (P1 != "")
            {
                strWhere += string.Format(" And ccxj.LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And  ccxj.ZhuYaoShiYou like '%{0}%'", P2);
            }
            DataTable dt = new JFBXManage().GetSPList("ZH_CCXJ", "ccxj.ID,ccxj.ShenQingRen,ccxj.BranchName,ccxj.LeiBie,ccxj.StarTime,ccxj.EndTime,ccxj.Daycount,ccxj.ZhuYaoShiYou", strWhere, UserInfo.User.UserName);
            msg.Result = dt; 
        }
     
        /// <summary>
        /// 我的请假
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETMYCCQJLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " ";
            if (P1 != "")
            {
                strWhere += string.Format(" And LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And  ZhuYaoShiYou like '%{0}%'", P2);
            }
            string strSQL = string.Format("select *   from ZH_CCXJ where CRUser='{0}' " + strWhere, UserInfo.User.UserName);
            msg.Result = new Yan_WF_TDB().GetDTByCommand(strSQL);
            msg.Result1 = new Yan_WF_TDB().GetTaskStatus(1);
        }
    }
    public class DFContent
    {
        public int PDId;
        public string spRole;
        public string spRole2;
        public bool isEnable;
        public string IsDetailUser;
    }
}